<template>
  <!--商品分类管理 -->
  <div class="dashboard-container">
    <SearchTable :show-pagination="false">
      <template slot="header">
        <el-row style="width: 100%">
          <el-input
            style="width: 300px"
            @input="searchKeyWord"
            clearable
            placeholder="请输入分类名搜索关键词"
            v-model="keyWord"
          ></el-input>
        </el-row>
        <el-button type="primary" @click="addStore">新增分类</el-button>
      </template>
      <template>
        <el-table :data="tableData" height="675" border style="width: 100%">
          <el-table-column type="index" label="序号" align="center" width="60"></el-table-column>
          <el-table-column prop="category_name" width="300" label="分类名" align="center"></el-table-column>
          <el-table-column prop="category_description" label="分类描述" align="center"></el-table-column>
          <el-table-column label="操作" width="180" align="center">
            <template v-slot="{ row }">
              <div class="link-list">
                <el-link :underline="false" type="primary" @click="edit(row)">编辑</el-link>
                <el-link :underline="false" type="danger" @click="deleteCategory(row)">删除</el-link>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </SearchTable>

    <!-- 新增分类弹框 -->
    <AddDialog
      :storeData="storeForm"
      :title="title"
      v-if="addVisible"
      :visible.sync="addVisible"
      @confirm="addStoreConfirm"
    ></AddDialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import SearchTable from "@/components/TableTemplate/SearchTable";
import { getAllCategories, deleteCategories } from "@/api/user";
import AddDialog from "./components/AddDialog";
export default {
  name: "Dashboard",
  components: {
    SearchTable,
    AddDialog,
  },
  data() {
    return {
      tableData: [],
      originTableData: [],
      addVisible: false,
      title: "新增分类",
      storeForm: {},
      keyWord: "",
      timer: null,
    };
  },
  mounted() {
    this.getAllCategories();
  },
  computed: {
    ...mapGetters(["name", "token"]),
  },
  methods: {
    async getAllCategories() {
      const { data: res } = await getAllCategories();
      this.tableData = res || [];
      this.originTableData = res || [];
    },
    searchKeyWord(keyWord) {
      if (this.timer) {
        clearTimeout(this.timer);
      }
      this.timer = setTimeout(() => {
        //这里setTimeout用来防抖，也就是500ms内一直在输入，就会清除上次的定时器，就不会一直搜索，等输入完了之后才真正执行一次搜索，优化
        //先复原
        this.tableData = this.originTableData;
        //再过滤
        let arr = [];
        this.tableData.map((item) => {
          if (item.category_name.indexOf(keyWord) !== -1) {
            arr.push(item);
          }
        });
        this.tableData = arr;
      }, 500);
    },
    addStore() {
      this.title = "新增分类";
      this.addVisible = true;
      this.storeForm = {};
    },
    edit(row) {
      this.title = "编辑分类";
      this.addVisible = true;
      this.storeForm = row;
    },
    addStoreConfirm() {
      //刷新列表
      this.getAllCategories();
    },
    //删除分类
    deleteCategory(row) {
      this.$confirm(
        "注意：正常情况下，禁止删除分类，请谨慎操作，是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(async () => {
          const res = await deleteCategories(row.category_id);
          if (res.status == 0) {
            this.getAllCategories();
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          } else {
            this.$message({
              type: "error",
              message: "删除失败!",
            });
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style lang="scss" scoped>
.dashboard-container {
  width: 100%;
  height: 100%;
}
.link-list {
  .el-link {
    margin: 0 2px;
  }
}
.img-box {
  margin-left: 65px;
  width: 80px;
  height: 120px;
  display: block;
}
</style>
